package com.example.text.http
import com.example.text.http.interceptor.SignInterceptor
import com.example.text.http.interceptor.TokenInterceptor
import dagger.Module
import dagger.Provides
import dagger.hilt.InstallIn
import dagger.hilt.components.SingletonComponent
import okhttp3.OkHttpClient
import okhttp3.logging.HttpLoggingInterceptor
import retrofit2.Retrofit
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory

import retrofit2.converter.gson.GsonConverterFactory
import java.util.concurrent.TimeUnit
import javax.inject.Inject
import javax.inject.Qualifier
import javax.inject.Singleton

/**
 * @author yaotianxue
 * @description
 * @date 2025/5/12
 */
@Module
@InstallIn(SingletonComponent::class)
class NetModule {
    @Provides
    @Singleton
    @CommonOkHttpClient
    fun provideOkHttpClient():OkHttpClient{
        return OkHttpClient.Builder()
            .readTimeout(60, TimeUnit.SECONDS)
            .writeTimeout(60, TimeUnit.SECONDS)
            .connectTimeout(60, TimeUnit.SECONDS)
            .addInterceptor(TokenInterceptor())
            .addInterceptor(SignInterceptor())
            .addInterceptor(HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BODY))
            .build()
    }
    @Provides
    @Singleton
    @TokenOkHttpClient
    fun provideTokenOkHttpClient():OkHttpClient{
        return OkHttpClient.Builder()
            .readTimeout(60, TimeUnit.SECONDS)
            .writeTimeout(60, TimeUnit.SECONDS)
            .connectTimeout(60, TimeUnit.SECONDS)
            .addInterceptor(TokenInterceptor())
            .addInterceptor(HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BODY))
            .build()
    }
    @Provides
    @Singleton
    @SignOkHttpClient
    fun provideSignOkHttpClient():OkHttpClient{
        return OkHttpClient.Builder()
            .readTimeout(60, TimeUnit.SECONDS)
            .writeTimeout(60, TimeUnit.SECONDS)
            .connectTimeout(60, TimeUnit.SECONDS)
            .addInterceptor(SignInterceptor())
            .addInterceptor(HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BODY))
            .build()
    }
    @Provides
    @Singleton
    @CommonRetrofit
    fun provideRetrofit(@CommonOkHttpClient client:OkHttpClient):Retrofit{
        return Retrofit.Builder()
            .baseUrl("http://10.161.9.80:8886")
            .addConverterFactory(GsonConverterFactory.create())
            .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
            .client(client)
            .build()
    }

    @Provides
    @Singleton
    @TokenRetrofit
    fun provideTokenRetrofit(@TokenOkHttpClient client:OkHttpClient):Retrofit{
        return Retrofit.Builder()
            .baseUrl("http://10.161.9.80:8886")
            .client(client)
            .addConverterFactory(GsonConverterFactory.create())
            .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
            .build()
    }


    @Provides
    @Singleton
    @SignRetrofit
    fun provideSignRetrofit(@SignOkHttpClient client:OkHttpClient):Retrofit{
        return Retrofit.Builder()
            .baseUrl("http://10.161.9.80:8886")
            .client(client)
            .addConverterFactory(GsonConverterFactory.create())
            .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
            .build()
    }
    @Provides
    @Singleton
    fun provideApi(@CommonRetrofit retrofit:Retrofit):ClubService{
        return  retrofit.create(ClubService::class.java)
    }

    /******限定符******///为了区分一样的网络请求（不区分的话，他不知道具体是哪个），所以使用@Qualifier
    @Qualifier
    @Retention(AnnotationRetention.BINARY)
    annotation class CommonOkHttpClient

    @Qualifier
    @Retention(AnnotationRetention.BINARY)
    annotation class TokenOkHttpClient

    @Qualifier
    @Retention(AnnotationRetention.BINARY)
    annotation class SignOkHttpClient

    @Qualifier
    @Retention(AnnotationRetention.BINARY)
    annotation class CommonRetrofit

    @Qualifier
    @Retention(AnnotationRetention.BINARY)
    annotation class TokenRetrofit

    @Qualifier
    @Retention(AnnotationRetention.BINARY)
    annotation class SignRetrofit


}